package com.rbn.stockmobiledevices.clientes;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class ClienteController {
	
	private ClienteManager clienteManager;
	
	@Autowired 
	public ClienteController(ClienteManager accountManager) {
		this.clienteManager = accountManager;
	}
		
	@RequestMapping(value="/getClienteDetail", method=RequestMethod.GET)
	public @ResponseBody Cliente getDetail(@RequestParam int id) {
		Cliente c = clienteManager.getById(id);
	    return c;
	}
	
	@RequestMapping(value="/removeCliente", method=RequestMethod.GET)
	public @ResponseBody Map<String, Boolean> remove(@RequestParam int id) {
		boolean result = clienteManager.remove(id);
		HashMap<String, Boolean> valueReturn = new HashMap<String, Boolean>();
		valueReturn.put("status", result);
	    return valueReturn;
	}
	
	@RequestMapping(value="/updateCliente", method=RequestMethod.GET)
	public @ResponseBody Map<String, Boolean> update(@RequestParam int id, String nombre, String telefono, String email) {
		Cliente cliente = clienteManager.getById(id);
		cliente.setNombre(nombre);
		cliente.setTelefono(telefono);
		cliente.setEmail(email);

		HashMap<String, Boolean> valueReturn = new HashMap<String, Boolean>();
		boolean result = true;
		
		try{
			clienteManager.update(cliente);
		}catch(Exception e){
			result = false;
		}
		valueReturn.put("status", result);
	    return valueReturn;
	}
	
	@RequestMapping(value="/addCliente", method=RequestMethod.GET)
	public @ResponseBody Map<String, Object> add(@RequestParam String nombre, @RequestParam String telefono, @RequestParam String email) {
		boolean result = true;
		Cliente cliente = new Cliente();
		Integer cliente_id = 0;
		try{
			cliente.setNombre(nombre);
			cliente.setTelefono(telefono);
			cliente.setEmail(email);
			cliente_id = clienteManager.save(cliente);
		}catch (Exception e) {
			result = false;
		}
		HashMap<String, Object> valueReturn = new HashMap<String, Object>();
		valueReturn.put("status", result);
		valueReturn.put("cliente_id", cliente_id);
		
	    return valueReturn;
	}
	
	@RequestMapping(value = "/clientList", method = RequestMethod.GET)
	public String clientList(Model model) {
		return "cliente.tiles";
	}
	
	@RequestMapping(value = "/clientes", method = RequestMethod.GET)
	public @ResponseBody
	Map<String, Object> getListaClientes(@RequestParam int iDisplayStart, @RequestParam int iDisplayLength,
			@RequestParam String sSearch, @RequestParam String sEcho) {

		Map<String, Object> map = new HashMap<String, Object>();
		List<Cliente> clientes = new ArrayList<Cliente>();
		int total = 0;
		int filtered = 0;
		try{
			clientes = clienteManager.getAll();
			total = clientes.size();
			filtered = total;
			
			if (sSearch != null && sSearch.trim().length() > 0) {
				filtered = clienteManager.getPaginadoClientes(null, null, sSearch).size();
			}
			clientes = clienteManager.getPaginadoClientes(iDisplayStart, iDisplayLength, sSearch);
		}catch(Exception e){
		}
		map.put("sEcho", sEcho);
		map.put("iTotalRecords", String.valueOf(total));
		map.put("iTotalDisplayRecords", String.valueOf(filtered));
		map.put("clientes", clientes);

		return map;
	}
}